

package com.linqingying.cangjie.ide.formatter

import com.linqingying.cangjie.lang.CangJieLanguage
import com.intellij.openapi.util.NlsContexts
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.psi.codeStyle.CommonCodeStyleSettings


class CangJieObsoleteCodeStyle : CangJiePredefinedCodeStyle(CODE_STYLE_TITLE, CangJieLanguage) {
    override val codeStyleId: String = CODE_STYLE_ID

    override fun apply(settings: CodeStyleSettings) {
        Companion.apply(settings)
    }

    companion object {
        val INSTANCE = CangJieObsoleteCodeStyle()

        const val CODE_STYLE_ID = "CANGJIE_OLD_DEFAULTS"
        const val CODE_STYLE_SETTING = "obsolete"
        val CODE_STYLE_TITLE
            @NlsContexts.ListItem
            get() = CangJieStyleBundle.message("list.item.cangjie.obsolete.intellij.idea.codestyle")

        fun apply(settings: CodeStyleSettings) {
            applyToCangJieCustomSettings(settings.cangjieCustomSettings)
            applyToCommonSettings(settings.cangjieCommonSettings)
        }

        fun applyToCangJieCustomSettings(cangjieCustomSettings: CangJieCodeStyleSettings, modifyCodeStyle: Boolean = true) {
            cangjieCustomSettings.apply {
                if (modifyCodeStyle) {
                    CODE_STYLE_DEFAULTS = CODE_STYLE_ID
                }

                CONTINUATION_INDENT_IN_PARAMETER_LISTS = true
                CONTINUATION_INDENT_IN_ARGUMENT_LISTS = true
                CONTINUATION_INDENT_FOR_EXPRESSION_BODIES = true
                CONTINUATION_INDENT_FOR_CHAINED_CALLS = true
                CONTINUATION_INDENT_IN_SUPERTYPE_LISTS = true
                CONTINUATION_INDENT_IN_IF_CONDITIONS = true
                CONTINUATION_INDENT_IN_ELVIS = true
                WRAP_EXPRESSION_BODY_FUNCTIONS = CodeStyleSettings.DO_NOT_WRAP
                IF_RPAREN_ON_NEW_LINE = false
            }
        }

        fun applyToCommonSettings(commonSettings: CommonCodeStyleSettings, modifyCodeStyle: Boolean = true) {
            commonSettings.apply {
                CALL_PARAMETERS_WRAP = CodeStyleSettings.DO_NOT_WRAP
                CALL_PARAMETERS_LPAREN_ON_NEXT_LINE = false
                CALL_PARAMETERS_RPAREN_ON_NEXT_LINE = false

                METHOD_PARAMETERS_WRAP = CodeStyleSettings.DO_NOT_WRAP
                METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE = false
                METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE = false

                EXTENDS_LIST_WRAP = CodeStyleSettings.DO_NOT_WRAP
                METHOD_CALL_CHAIN_WRAP = CodeStyleSettings.DO_NOT_WRAP
                ASSIGNMENT_WRAP = CodeStyleSettings.DO_NOT_WRAP
            }

            if (modifyCodeStyle && commonSettings is CangJieCommonCodeStyleSettings) {
                commonSettings.CODE_STYLE_DEFAULTS = CODE_STYLE_ID
            }
        }
    }
}
